package com.bobby.web;

import com.bobby.pojo.Book;
import com.bobby.pojo.Page;
import com.bobby.service.BookService;
import com.bobby.service.impl.BookServiceImpl;
import com.bobby.utils.JdbcUtils;
import com.bobby.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description TODO
 * @Date 2020/8/14 16:44
 * @Created wenjunpei
 */
public class BookServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    protected void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String pageNoStr = request.getParameter("pageNo");
        Integer pageNo = Integer.parseInt(pageNoStr) + 1;
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), Book.class);

        bookService.addBook(book);

        String root = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
        response.sendRedirect(root + "manager/bookServlet?action=page&pageNo=" + pageNo);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);

        bookService.deleteBookById(id);

        String root = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
        response.sendRedirect(root + "manager/bookServlet?action=page&pageNo="+ request.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), Book.class);

        bookService.updateBook(book);

        String root = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
        response.sendRedirect(root + "manager/bookServlet?action=page&pageNo=" + request.getParameter("pageNo"));
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Book> books = null;

        books = bookService.queryBooks();

        request.setAttribute("bookList",books);
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }

    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);
        Book book = null;

        book = bookService.queryBookById(id);

        request.setAttribute("book",book);
        request.setAttribute("pageNo",request.getParameter("pageNo"));
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
    }

    protected void page(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        Page page = null;

        page = bookService.page(pageNo,pageSize);

        page.setUrl("manager/bookServlet?action=page");
        request.setAttribute("page",page);
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }
}
